<template>
	<div class="passwordBox" style=" width: 500px; margin: 30px 20px;">
	<!-- <h4>修改初始密码</h4> -->
		<el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="userpassword">
          <el-input type="password"  v-model="ruleForm2.userpassword" auto-complete="off"></el-input>
        </el-form-item>
			  <el-form-item label="新密码" prop="pass">
			    <el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
			  </el-form-item>
			  <el-form-item label="确认新密码" prop="checkPass">
			    <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
			  </el-form-item>
			  <el-form-item>
			    <el-button type="primary" :loading="loading" @click="submitForm('ruleForm2')">提交</el-button>
			    <el-button @click="resetForm('ruleForm2')">重置</el-button>
			  </el-form-item>
		</el-form>
	</div>
</template>


<script>
import axios from "axios";
import apiBase from "@/api/apiBase.js";
export default {
  name: "password",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        callback(new Error("原密码不能为空"));

      }
      setTimeout(() => {
        if (value < 0) {
          callback(new Error("密码必须大于6位数"));
        } else {
          callback();
        }
      }, 1000);
    };

    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else if( this.$store.state.loginPassword === this.ruleForm2.pass ){
        callback(new Error("与原密码相同，请再次输入密码"));
       }else {
        callback();
      }
    };

    return {
      loading: false,
      ruleForm2: {
        pass: "",
        checkPass: "",
        old: "",
        userpassword: "",
        loginPassWord: ''
      },
      rules2: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        userpassword: [{ validator: checkAge, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;

          let data = {
            //请求体
            USRPASSWD: this.ruleForm2.userpassword,
            USRNEWUSRPASSWD: this.ruleForm2.pass
          };

          apiBase.DoService(
            data,
            apiBase.getApiAdress().Login.UpdatePassword,
            res => {
              this.loading = false;
              this.$message({
                message: "成功",
                type: "success"
              });
              // console.log( res )
              // this.loginPassWord = this.ruleForm2.pass
              this.$store.state.loginPassword = this.ruleForm2.pass
              // var password = localStorage.setItem(
              //   "password",
              //   this.ruleForm2.pass
              // );

              this.$router.push({ path: "/dashboard" });
              (this.ruleForm2.userpassword = ""),
                (this.ruleForm2.pass = ""),
                (this.ruleForm2.checkPass = "");
            }, res =>{
              this.ruleForm2.userpassword = ''
              this.$message.error(res.data.msg);
              //alert(res.data.msg)
              this.loading = false;
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created(){

   //this.loginPassWord = localStorage.getItem('password')

   }
};
</script>